#!/bin/bash
source ../base_model.sh

function converter_origin_model_to_ms_model() {
    if [ ${ms_quant_flag^^} = "ON" ]; then
        ms_convert_append=" ${ms_convert_append} --configFile=${model_root_pos}/quant_config.cfg "
    else
        ms_convert_append=" ${ms_convert_append} "
    fi

    echo "export LD_LIBRARY_PATH=${ms_converter_pos}/lib/" > ${cur_model_pos}/do_convert_cmd.sh
    export LD_LIBRARY_PATH=${ms_converter_pos}/lib/
    echo "###################################################### Transform ${cur_model_type^^} Begin ##################################################################"
    if [ ${cur_model_type^^} = "CAFFE" ]; then
        echo "##### 1. Caffe ##########################"
        echo "${ms_converter_pos}/converter/converter_lite --fmk=${cur_model_type^^} --modelFile=${cur_model_pos}/${cur_model_file}.prototxt --weightFile=${cur_model_pos}/${cur_model_file}.caffemodel --outputFile=${cur_model_pos}/${cur_ms_model_file} ${ms_convert_append}"  >> ${cur_model_pos}/do_convert_cmd.sh
        ${ms_converter_pos}/converter/converter_lite --fmk=${cur_model_type^^} --modelFile=${cur_model_pos}/${cur_model_file}.prototxt --weightFile=${cur_model_pos}/${cur_model_file}.caffemodel --outputFile=${cur_model_pos}/${cur_ms_model_file} ${ms_convert_append}
    elif [ ${cur_model_type^^} = "PB" ]; then
        echo "##### 2. Tensorflow ##########################"
        # converter_lite --fmk="TF" --modelFile=${cur_model_pos}/${cur_model_file} --outputFile=${cur_model_pos}/${cur_model_file} --inputShape=${model_input_shapes}
        echo "${ms_converter_pos}/converter/converter_lite --fmk="TF" --modelFile=${cur_model_pos}/${cur_model_file} --outputFile=${cur_model_pos}/${cur_ms_model_file} ${ms_convert_append}" >> ${cur_model_pos}/do_convert_cmd.sh
        ${ms_converter_pos}/converter/converter_lite --fmk="TF" --modelFile=${cur_model_pos}/${cur_model_file} --outputFile=${cur_model_pos}/${cur_ms_model_file} ${ms_convert_append}
    else
        echo "##### 3. Other ##########################"
        echo "${ms_converter_pos}/converter/converter_lite --fmk=${cur_model_type^^} --modelFile=${cur_model_pos}/${cur_model_file} --outputFile=${cur_model_pos}/${cur_ms_model_file} ${ms_convert_append}" >> ${cur_model_pos}/do_convert_cmd.sh
        ${ms_converter_pos}/converter/converter_lite --fmk=${cur_model_type^^} --modelFile=${cur_model_pos}/${cur_model_file} --outputFile=${cur_model_pos}/${cur_ms_model_file} ${ms_convert_append}
    fi

    if [ $? -ne 0 ]; then
        echo "###################################################### Transform fail ##################################################################"
        exit 1
    fi
    echo "###################################################### Transform ${cur_model_file} Finish ##################################################################"
}

converter_origin_model_to_ms_model
